<?php
/**
 * Created by PhpStorm.
 * User: Admin
 * Date: 2017-12-22
 * Time: 下午 10:30
 */
// error_reporting(E_ALL ^ E_NOTICE);
require_once 'JKSocket.php';

$server = new JKSocket(8800);
$server->onListen(function($obj, $args) {
    echo "Listen ..\n";
});

$server->onConnect(function (JKSocket $obj, $args){
    /** @var JKSocket $client */
    $client = $args['client'];
    echo "New clinet #{$client->getIndex()} ({$client->getAddr()}) connected\n";
    
    $client->send("Hello\n");
});

$server->onClose(function (JKSocket $obj, $args) {
    /** @var JKSocket $client */
    $client = $args['client'];
    echo "Clinet #{$client->getIndex()} ({$client->getAddr()}) closed\n";
});

$server->onRecv(function (JKSocket $obj, $args) {
    /** @var JKSocket $client */
    $client = $args['client'];
    $data = $args['data'];
    echo "#{$client->getIndex()} ({$client->getAddr()}):" . $data . "\n";
});


if($server->listen()->fail()) {
    echo $server->getLastError();
}